<template>
  <div>
    <v-back></v-back>

    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="changeadd"
      ><van-empty description="暂无地址 快去添加吧~" v-show="list.length == 0"
    /></van-address-list>
  </div>
</template>


<script>
import { addresslist } from "../request/app.js";
export default {
  data() {
    return {
      list: [],
      chosenAddressId: 0,
    };
  },
  methods: {
    onAdd() {
      this.$router.push({
        path: "/addressadd",
        query: {
          type: 1,
        },
      });
    },
    onEdit(item) {
      this.$router.push({
        path: "/addressadd",
        query: {
          type: 2,
          id:item.id
        },
      });
    },
    changeadd(item){
      this.$router.push({
      path:"/order",
        query:{
          item:encodeURI(JSON.stringify(item))
        }
      });
    }
  },
  mounted() {
    addresslist({
      uid: JSON.parse(localStorage.getItem("info")).uid,
    }).then((val) => {
      if (val.code != 200) {
        this.$toast(val.msg);
        return;
      }
      if (val.list == null) {
        val.list = [];
      }
      this.list = val.list.map((item) => {
        return {
          id: item.id,
          name: item.username,
          tel: item.userphone,
          address: item.location + item.useraddress,
          isDefault: item.status ? true : false,
        };
      });
    });
  },
};
</script>


<style scoped>
</style>